#include "settingwidget.h"
#include "mybtn/backbtn.h"
#include "commondef.h"
#include <QPainter>
#include "global/globalvar.h"
#include <QMouseEvent>
#include <QLineEdit>

SettingWidget::SettingWidget(QWidget *parent) :
    QWidget(parent)
{
    m_backbtn = new BackBtn(this);
    m_backbtn->setGeometry(0,WINDOW_HEIGHT - 60,80,60);
    connect(m_backbtn,SIGNAL(myclick()),this,SLOT(settomain()));

    m_edit = new QLineEdit(this);
    m_edit->setGeometry(150,100,100,40);
}

void SettingWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.drawText(QRectF(20.0f,100.0f,100.0f,40.0f),
               Qt::AlignCenter,
               QString(tr("petname")) + QString(" :"));

    p.drawText(QRectF(20.0f,200.0f,100.0f,40.0f),
               Qt::AlignCenter,
               QString(tr("voice")) + QString(" :"));

    p.drawText(QRectF(150.0f,200.0f,100.0f,40.0f),
               Qt::AlignCenter,
               globalvar::m_bVoice ? QString(tr("On")) : QString(tr("Off")));
}

void SettingWidget::mousePressEvent(QMouseEvent *e)
{
    if(QRectF(150.0f,200.0f,100.0f,40.0f).contains(e->posF()))
    {
        globalvar::m_bVoice = !globalvar::m_bVoice;
        update();
    }
    else if(QRectF(0,WINDOW_HEIGHT - 60,80,60).contains(e->posF()))
    {
        if(globalvar::m_bVoice != globalvar::IsVoice())
        {
            globalvar::SetVoice(globalvar::m_bVoice);
        }

        if(m_edit->text() != globalvar::name())
        {
            globalvar::setName(m_edit->text());
        }

        emit stom();
    }
}

void SettingWidget::setEdit()
{
    m_edit->setText(globalvar::name());
}

void SettingWidget::settomain()
{
    if(globalvar::m_bVoice != globalvar::IsVoice())
    {
        globalvar::SetVoice(globalvar::m_bVoice);
    }

    if(m_edit->text() != globalvar::name())
    {
        globalvar::setName(m_edit->text());
    }

    emit stom();
}
